package com.xiaobu.chart;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.achartengine.GraphicalView;

import com.xiaobu.go.R;
import com.xiaobu.go.StepDatabase;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.view.View;

public class ChartBuilder extends Activity
{
	private static final int FLAG_CHART_DAY = 0;
	private static final int FLAG_CHART_WEEK = 1;
	private static final int FLAG_CHART_MONTH = 2;
	private static final int FLAG_CHART_YEAR = 3;
	
	private StepDatabase mDB;
	
	private IChart mChart;
	private GraphicalView mChartView;
	
	private int mFlagChartMode = 0;
	
	private boolean mScreenLarge = true;
	List<double[]> mValues = new ArrayList<double[]>();
	List<double[]> mX = new ArrayList<double[]>();
	
	Button btn_day;
	Button btn_week;
	Button btn_month;
	
	@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chart);
        
        btn_day = (Button) this.findViewById(R.id.day);
        btn_day.setOnClickListener(new Button.OnClickListener(){
        	public void onClick(View v) {
        		Log.v("ChartBuilder", "onClick-id,day");
        	}
        });
        
        btn_week = (Button) this.findViewById(R.id.week);
        btn_week.setOnClickListener(new Button.OnClickListener(){
        	public void onClick(View v) {
        		Log.v("ChartBuilder", "onClick-id,week");
        	}
        });
        
        
        btn_month = (Button) this.findViewById(R.id.month);
        btn_month.setOnClickListener(new Button.OnClickListener(){
        	public void onClick(View v) {
        		Log.v("ChartBuilder", "onClick-id,month");
        	}
        });
        
    }
	
	@Override
	public void onDestroy()
	{
	    super.onDestroy();
	}
	
	@Override
    protected void onPause()
	{
		super.onPause();
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState)
	{
		super.onRestoreInstanceState(savedInstanceState);
	}
	
	@Override
    protected void onResume()
	{
		super.onResume();
		
		this.mDB = new StepDatabase(this);
		
		updateChart();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);
	}
	
	private void dayChart()
	{
		setDayData();
		
		LinearLayout linearLayout = (LinearLayout)findViewById(R.id.chart);
		
	    this.mChart = new DayLineChart();
	    
	    if (this.mChartView != null)
		{
			linearLayout.removeView(this.mChartView);
		}
	    
	    //this.mChartView = this.mChart.graphicalView(this, this.mX, this.mValues, this.mScreenLarge);
	    this.mChartView = this.mChart.testView(this);
	    
	    linearLayout.addView(this.mChartView, new ViewGroup.LayoutParams(-1, -1));
	}
	
	private void yearChart()
	{
		setYearData();
		
		LinearLayout linearLayout = (LinearLayout)findViewById(R.id.chart);
		
	    this.mChart = new YearBarChart();
	    
	    if (this.mChartView != null)
		{
			linearLayout.removeView(this.mChartView);
		}
	    
	    this.mChartView = this.mChart.graphicalView(this, this.mX, this.mValues, this.mScreenLarge);
	    
	    linearLayout.addView(this.mChartView, new ViewGroup.LayoutParams(-1, -1));
	}
	
	private void monthChart()
	{
		setMonthData();
		
		LinearLayout linearLayout = (LinearLayout)findViewById(R.id.chart);
		
		this.mChart = new MonthBarChart();
		
		if (this.mChartView != null)
		{
			linearLayout.removeView(this.mChartView);
		}
		
		this.mChartView = this.mChart.graphicalView(this, this.mX, this.mValues, this.mScreenLarge);
		
	    linearLayout.addView(this.mChartView, new ViewGroup.LayoutParams(-1, -1));
	}
	
	private void weekChart()
	{
		setWeekData();
		
		LinearLayout linearLayout = (LinearLayout)findViewById(R.id.chart);
		
		this.mChart = new WeekBarChart();
		
		if (this.mChartView != null)
		{
			linearLayout.removeView(this.mChartView);
		}
		
		this.mChartView = this.mChart.graphicalView(this, this.mX, this.mValues, this.mScreenLarge);
		
	    linearLayout.addView(this.mChartView, new ViewGroup.LayoutParams(-1, -1));
	}
	
	private void updateChart()
	{
		switch (this.mFlagChartMode)
		{
		case FLAG_CHART_DAY:
			dayChart();
			Log.v("ChartBuilder", "FLAG_CHART_DAY");
			break;
		case FLAG_CHART_WEEK:
			weekChart();
			break;
		case FLAG_CHART_MONTH:
			monthChart();
			break;
		case FLAG_CHART_YEAR:
			yearChart();
			break;
		}
	}
	
	private static final int CHART_HOUR = 24;
	
	public void setDayData()
	{
		Calendar calendar = Calendar.getInstance();
		Cursor cursor = this.mDB.getDaySteps(calendar.get(Calendar.YEAR), 1 + calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE));
		startManagingCursor(cursor);
		
		double[] arrayOfDouble1 = new double[CHART_HOUR + 1];
	    double[] arrayOfDouble2 = new double[CHART_HOUR + 1];
	    double[] arrayOfDouble3 = new double[CHART_HOUR + 1];
	    double[] arrayOfDouble4 = new double[CHART_HOUR + 1];
	    
	    for (int index = 0; index < CHART_HOUR; index++)
	    {
	    	arrayOfDouble1[index] = index + 1;
	    	arrayOfDouble2[index] = 1.7976931348623157E+308D;
	    	arrayOfDouble3[index] = index + 1;
	    	arrayOfDouble4[index] = 10000;
	    }
	    
	    if (cursor.moveToFirst())
	    {
	    	do
	    	{
	    		int hour = cursor.getInt(cursor.getColumnIndex("hour"));
	            int minute = cursor.getInt(cursor.getColumnIndex("minute"));
	            int steps = cursor.getInt(cursor.getColumnIndex("steps"));
	            
	            if ((hour < 23) && (minute < 59))
	            {
	            	//
	            }
	            
	            Integer.valueOf(steps).doubleValue();
	            
	    	} while (cursor.moveToNext());
	    }
	}
	
	public void setMonthData()
	{
		//
	}
	
	public void setWeekData()
	{
		//
	}
	
	public void setYearData()
	{
		//
	}
}